/*
 * MissedDeliveryNotificationJob.java
 *
 * Created on May 7, 2003, 11:54 AM
 */

package com.scalar.chiptrack.adaptor.jobs;

/**
 *
 * @author  Hareesh
 * @author Balaram Putta
 * @version
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.UserGroup;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.Mailer;
import com.scalar.chiptrack.utils.StringUtils;

public class ShippingDelayedNotificationJob extends JobHandler{

    /** Creates new ShippingDelayedNotificationJob */
    public ShippingDelayedNotificationJob() {
        //nothing
    }
    
    public void execute() throws Exception {
        
        /*ArrayList shippedDelayedLotList = null;
        ArrayList userList = new ArrayList();
        try{
            userList = UserDAO.loadUsersByGroupName (UserGroup.USER_GROUP_OPERATIONS, false);
            if (userList.isEmpty() == true) {
                System.out.println ("There are zero users to mail the Shipping Delay list...");
                return;
            }
            
            System.out.println ("number of users = " + userList.size());
            
            shippedDelayedLotList = ShippedLotDAO.loadAllShippingDelayedLots();
            
            if ((shippedDelayedLotList == null) || (shippedDelayedLotList.size() == 0)) {
                return;
            }
            
            System.out.println ("number of Shipping Delayed Lots = " + shippedDelayedLotList.size());
            
            sendAlert (shippedDelayedLotList, userList);
        }
        catch (Exception e) {
            System.err.println ("Exception while running ShippingDelayedNotificationJob: " + e);
            e.printStackTrace();
            throw e;
        }*/
    }
    
    private void sendAlert (ArrayList shippedDelayedLotList, ArrayList userList) throws Exception {
            
        Iterator users = userList.iterator();
        User user = null;
        String email = null;
        StringBuffer emails = new StringBuffer();

        while (users.hasNext()) {
            user = (User) users.next();
            email = user.getEmail();

            if (email != null) {
                emails.append (email).append (Mailer.EMAIL_SEPARATOR);
                               
                // for debugging 
                System.out.println(email);
            }
        }

        String bcc = emails.toString();
        if ((bcc == null) || (bcc.trim().length() == 0)) {
            return;
        }
     
        String from = ConfigManager.getSysAdminEmail();
        String to =  ConfigManager.getSysAdminEmail();
        String subject = "Shipping Delayed Lots Notification as of " +  StringUtils.formatDateToString (new Date (System.currentTimeMillis()));
        String message = createMessageBody (shippedDelayedLotList);

        System.out.println ("from - " + from + ", to - " + to);
        Mailer.send (from, to, null /*cc*/, bcc, subject, message, null /*fileAttachments*/, Mailer.CONTENT_TYPE_HTML);

    }
    
    private String createMessageBody (ArrayList shippedDelayedLotList) {
                        
        StringBuffer messageBody = new StringBuffer();
        Iterator shippedDelayedLotItems = shippedDelayedLotList.iterator();
        ShippedLot shippedLot = null;
        int i=0;
        String bgColor = null;
        
        // html page design -- start
        
        messageBody.append ("<html><head>\n");
        messageBody.append ("<style type=\"text/css\">");
        messageBody.append ("#toc a{ font-family: Verdana;font-style : normal ;font-size : 9pt; font-weight :bold;}");
        messageBody.append ("#toc p{font-family: Verdana;font-style : normal ;font-size : 9pt; font-weight :bold;text-align :center;color :#FFFFFF}");
        messageBody.append ("#toc {border-style : solid;border-color :#000000 ;border-width :medium }");
        messageBody.append ("</style></head><body bgcolor='FDF5E6'>");
        // table -- start
        messageBody.append("<TABLE width='80%' align=center border=0><TBODY>");
        messageBody.append("<TR><TD align=center bgColor=#993333 colSpan=2>");
        messageBody.append("<FONT color=#ffffff><FONT size=5><STRONG>Shipping Delayed Lots as of ").append(StringUtils.formatDateToString (new Date (System.currentTimeMillis()))).append("</STRONG></FONT></FONT></TD></TR>");
        messageBody.append("<TR><TD colSpan=2><BR></TD></TR>");
        messageBody.append("<TR bgColor=#dee1e2><TD colSpan=2><BR>");
        messageBody.append("<TABLE cellSpacing=0 cellPadding=0 width='100%' align=center bgColor=#ffffff border=1><TBODY>");
        messageBody.append("<TR><TD><TABLE cellSpacing=3 cellPadding=2 width='100%' align=center bgColor=#ffffff border=0><TBODY>");
        messageBody.append("<TR><TD align=center colSpan=6><BR></TD></TR>");
        messageBody.append("<TR><TD colSpan=6>");
        messageBody.append("<TABLE borderColor=#000000 cellSpacing=0 cellPadding=0 width='100%' border=1 bordercolor='#000000'><TBODY><TR>");
        messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff>Part<br>Number</FONT></TD>");
        messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff>Lot<br>Number</FONT></TD>");
        messageBody.append("<TD align=center width='20%' bgColor=#993333><FONT color=#ffffff>Comming From</FONT></TD>");
        messageBody.append("<TD align=center width='20%' bgColor=#993333><FONT color=#ffffff>Going To</FONT></TD>");
        messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff>Shipped<br>Quantity</FONT></TD>");
        messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff>Shipped<br>Date</FONT></TD>");
        messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff>ETD*</FONT></TD>");
        messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff>ETA*</FONT></TD>");
        messageBody.append("</TR>");
                      
        while (shippedDelayedLotItems.hasNext()) {
                       
            if (i%2 == 0) bgColor = "#DEE1E2";
            else bgColor = "#FFFFEE";

            shippedLot = (ShippedLot) shippedDelayedLotItems.next();
            //System.out.println (shippedLot);
            
            // have to take care of the null fields, date format and Qty format.
            
            messageBody.append ("<tr>");
            messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getPartNumber()).append("&nbsp;</td>");
            messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getLotNumber() ).append("&nbsp;</td>");
            messageBody.append ("<TD align=left  width='20%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getCurrentLocation()).append("&nbsp;</td>");
            messageBody.append ("<TD align=left  width='20%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getNextLocation()).append("&nbsp;</td>");
            messageBody.append ("<TD align=right  width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getShipQty()).append("&nbsp;</td>");
            messageBody.append ("<TD align=center  width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getShipDate()).append("&nbsp;</td>");
            messageBody.append ("<TD align=center  width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append((shippedLot.getEtd()==null)?"&nbsp":shippedLot.getEtd()).append("&nbsp;</td>");
            messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append((shippedLot.getEta()==null)?"&nbsp":shippedLot.getEta()).append("&nbsp;</td>");
            messageBody.append ("</tr>");
            i++;   
             
        }
        
        messageBody.append ("</TBODY></TABLE></TD></TR>");
        messageBody.append ("<TR><TD align=left colSpan=6><BR><font size=1>");
        messageBody.append ("*&nbsp;ETD : Expected Time of Delivery &nbsp;*&nbsp; ETA : Expected Time of Arrival <br>");
        messageBody.append ("</font></TD></TR></TBODY></TABLE>");
        messageBody.append ("</TD></TR></TBODY></TABLE><BR>");
            
         // footer start
            
        messageBody.append ("<TABLE width='65%' border=0><TBODY>");
        messageBody.append ("<TR><TD>__________________________________________</TD></TR>");
        messageBody.append ("<TR><TD><FONT size=2>You are receiving this email because you have opted for it. <br>If you no longer wish");
        messageBody.append ("you receive this email. Please contact<br>the system administrator.</FONT></TD></TR>");
        messageBody.append ("<TR><TD><FONT size=2></FONT></TD></TR>");
        messageBody.append ("<TR><TD><FONT size=2></FONT></TD></TR>");
        messageBody.append ("</TBODY></TABLE><BR><BR></TD></TR></TBODY></TABLE>");
            
            // footer end
            
        messageBody.append ("</BLOCKQUOTE></BODY></HTML>");
            
            // html page design end
        
        return messageBody.toString();
    }
    
    public static void main(String agrs[]) throws Exception{
        System.setProperty("cmd", "");
        System.setProperty("scalar.properties", "c:/chiptrack/config/scalar.properties");
        new ShippingDelayedNotificationJob().execute();
    }
}